package com.itcast.controller;

import com.alibaba.dubbo.config.annotation.Reference;
import com.itcast.domain.Student;
import com.itcast.service.StudentSevice;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.servlet.ModelAndView;


import java.util.List;

@RestController
public class HelloController {
    @Reference
    StudentSevice studentSevice;



    @RequestMapping("/findAll")
    @ResponseBody
    private ModelAndView list(ModelAndView modelAndView){
        System.out.println("sdada");
        List<Student> all = studentSevice.findAll();
        modelAndView.setViewName("/pages/list.jsp");
        modelAndView.addObject("list",all);
        return modelAndView;
}
    @RequestMapping("/add")
    public String add( Student student){
        System.out.println(student);
        studentSevice.add(student);
        return "forward:/findAll";
    }
    @RequestMapping("/findById")
    public ModelAndView update(ModelAndView modelAndView,int id){
        Student student=studentSevice.update(id);
        System.out.println("sss");
        modelAndView.setViewName("/pages/edit.jsp");
        modelAndView.addObject("student",student);
        return modelAndView;


    }
    @RequestMapping("/update")
    public String update2(Student student){
        studentSevice.update2(student);
        return "redirect:/findAll";

    }

    @RequestMapping("/delete")
    public String delete(int id){
        studentSevice.delete(id);
        return "redirect:findAll";

    }
}
